package cn.lutu.protocol.core;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class TimeMetaObjectHandler implements MetaObjectHandler {

    public static final String FIELD_CREATE_TIME = "createTime";
    public static final String FIELD_UPDATE_TIME = "updateTime";
    public static final String FIELD_POINTS = "points";
    public static final String FIELD_ROLE_ID = "roleId";
    public static final String FIELD_ROLE_NAME = "roleName";


    public TimeMetaObjectHandler(){
        log.info("开始时间自动填充!");
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        Integer p = 0;
        Long roleId = 2L;
        String roleName = "普通会员";
        log.info("开始注入时间,时间为:{}",now);
        this.setFieldValByName(FIELD_CREATE_TIME, now, metaObject);
        this.setFieldValByName(FIELD_UPDATE_TIME, now, metaObject);
        this.setFieldValByName(FIELD_POINTS,p,metaObject);
        this.setFieldValByName(FIELD_ROLE_ID,roleId,metaObject);
        this.setFieldValByName(FIELD_ROLE_NAME,roleName,metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        this.setFieldValByName(FIELD_UPDATE_TIME, now, metaObject);
    }

}
